package cn.zzf.leetcode;

public class T0008_Atoi {


    public static int myAtoi(String s) {
        char[] ca = s.toCharArray();
        int res = 0;
        boolean negative = false;
        boolean notBegin = true;
        for (char c : ca) {
            if (notBegin && c == ' ')
                continue;
            if (c == '0' && res == 0) {
                notBegin = false;
                continue;
            }
            if (c == '-') {
                if (notBegin) {
                    notBegin = false;
                    negative = true;
                    continue;
                } else {
                    break;
                }
            }
            if (c == '+') {
                if (notBegin) {
                    notBegin = false;
                    continue;
                } else {
                    break;
                }
            }
            if (c < '0' || c > '9') break;
            int i = c - '0';
            if (!negative && (res > 214748364  || (res == 214748364 && i >= 7))) {
                return Integer.MAX_VALUE;
            }
            //判断是否 小于 最小32位整数
            if (negative && (res > 214748364 || (res == 214748364 && i >= 8))) {
                return Integer.MIN_VALUE;
            }
            res = res * 10 + i;
            notBegin = false;
        }
        return negative ? -res : res;
    }

    public static void main(String[] args) {
        System.out.println(myAtoi("-21474836482"));
    }

}
